// pages/me/order/order.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
const http = new Http()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    type: 1,
    tabs: [
      {
        title: '待付款',
        type: 1
      },
      {
        title: '待发货',
        type: 2
      },
      {
        title: '配送中',
        type: 3
      },
      {
        title: '已完成',
        type: 4
      }
    ],
    showLogistics: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentTab: options.currentTab
    })
    this.logisticsDialog = this.selectComponent("#logisticsDialog")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.selectC
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 自定义方法
   */
  swichNav: function (e) {
    var current = e.currentTarget.dataset.current;
    if (this.data.currentTab != current) {
      this.setData({
        currentTab: current
      })
    }
  },

  switchTab: function (e) {
    var currentTab = e.detail.current
    this.setData({
      currentTab: currentTab
    })
    var currentPage = this.selectComponent("#orderList" + currentTab)
    currentPage.getOrderList(false, false)
  },

  hideMask: function(){
    this.logisticsDialog.hideDialog()
  },

  showLogistics: function(e){
    var selectOrder = e.detail.selectOrder
    this.setData({
      selectOrder: selectOrder
    })
    this.logisticsDialog.showDialog()
  },

  repay: function (e) {
    wx.showLoading({
      title: '支付中...',
    })
    var that = this
    var order = e.detail.order
    http.sendHttp(Api.repay_order, {
      data: {
        orderNo: order.orderNo
      },
      success: function (res) {
        var orderNo = res.data.obj.orderNo
        wx.requestPayment({
          timeStamp: res.data.obj.timeStamp,
          nonceStr: res.data.obj.nonceStr,
          package: res.data.obj.package,
          signType: res.data.obj.signType,
          paySign: res.data.obj.paySign,
          success: function (res) {
            that.queryOrder(orderNo)
            wx.redirectTo({
              url: '/pages/mall/paySuccess/paySuccess?success=1&orderNo=' + orderNo,
            })
          },
          fail: function (res) {
            
          }
        })
      },
      finish: function () {
        wx.hideLoading()
      }
    }, 'POST')
  },
  queryOrder: function (orderNo) {
    http.sendHttp(Api.query_order, {
      data: {
        orderNo: orderNo
      },
      success: function (res) {

      }
    }, 'POST')
  },
  tapCopy: function(){
    var selectOrder = this.data.selectOrder
    wx.setClipboardData({
      data: selectOrder.logisticsNo,
      success: function() {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        })
      }
    })
  }
})